package com.mtk.launcher.power.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cdl.library.dialog.OridinryDialog;
import com.cdl.library.listener.OridinryDialogClick;
import com.mtk.launcher.R;
import com.mtk.launcher.activity.BaseActivity;
import com.mtk.launcher.power.adapter.PowerSaveAdapter;
import com.mtk.launcher.power.bean.PowerLogEntity;
import com.mtk.launcher.power.util.PowerOnOffUtil;

import java.util.ArrayList;
import java.util.List;

/***
 * 定时开关机日志界面
 */
public class PowerInOffLogActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_hiddle_view);
        initView();
    }

    ListView list_info;
    List<PowerLogEntity> list_log = new ArrayList<>();
    PowerSaveAdapter adapter;
    private TextView btn_clear;

    LinearLayout ll_back;

    private void initView() {
        TextView textView = findViewById(R.id.title);
        textView.setText("开关机日志");
        list_info = (ListView) findViewById(R.id.list_hiddle);
        adapter = new PowerSaveAdapter(this, list_log);
        list_info.setAdapter(adapter);
        btn_clear = (TextView) findViewById(R.id.btn_clear);
        btn_clear.setOnClickListener(this);

        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        ll_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.btn_clear:
                showDelAllLogDialog();
                break;
        }
    }

    private void showDelAllLogDialog() {
        OridinryDialog oridinryDialog = new OridinryDialog(PowerInOffLogActivity.this);
        oridinryDialog.setOnDialogClickListener(new OridinryDialogClick() {
            @Override
            public void sure() {
                PowerOnOffUtil.clearAllTimeLogDb();
                list_log.clear();
                adapter.setList(list_log);
            }

            @Override
            public void noSure() {

            }
        });
        oridinryDialog.show(getString(R.string.clear_all), getString(R.string.clear_all_content));
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDataInfo();
    }

    private void getDataInfo() {
        list_log.clear();
        adapter.setList(list_log);
        list_log = PowerOnOffUtil.getDbLogList();
        if (list_log == null || list_log.size() < 1) {
            showToastView("没有数据");
            return;
        }
        adapter.setList(list_log);
    }

}
